package net.mikoo.seals.boss.biz.services.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.mikoo.seals.boss.biz.mapper.client.DevicesModelMapper;
import net.mikoo.seals.boss.biz.services.DevicesModelServices;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.model.DevicesModel;



@Service
public class DevicesModelServicesImpl extends BaseServices implements DevicesModelServices {
	
	Logger logger = LoggerFactory.getLogger(DevicesModelServicesImpl.class);

	@Autowired
	private DevicesModelMapper devicesModelMapper;
	
	@Override
	public void save(DevicesModel devicesModel) {
		
		if(devicesModel == null){
			throw new ParametersException("devicesModel is null!");
		}
		devicesModel.setCreateTime(new Date());
		devicesModelMapper.insert(devicesModel);
	}

	@Override
	public void modify(DevicesModel devicesModel) {
		
		if(devicesModel == null){
			throw new ParametersException("devicesModel is null!");
		}
		devicesModelMapper.delete(devicesModel);
	}
	
	@Override
	public DevicesModel getInfoById(String model) {
		
		if(model == null){
			throw new ParametersException("DevicesModelId is null!");
		}
		
		DevicesModel DevicesModel = devicesModelMapper.selectInfoById(model);
		return DevicesModel;
	}

	@Override
	public List<DevicesModel> getList() {
		List<DevicesModel> list = devicesModelMapper.selectList();
		return list;
	}

	@Override
	public void publish(DevicesModel devicesModel) {
		
		if(devicesModel == null) {
			throw new ParametersException("devicesModel is null");
		}
		
		if(StringUtils.isBlank(devicesModel.getModel()) ) {
			throw new ParametersException("Model id is Blank");
		}
		
		if(StringUtils.isBlank(devicesModel.getDrivePath()) ) {
			throw new ParametersException("请上传驱动");
		}
		if(devicesModel.getVersionBase() == null ) {
			throw new ParametersException("主版本号不能为空");
		}
		if(devicesModel.getVersionAlpha() == null ) {
			throw new ParametersException("子版本号不能为空");
		}
		
		if(devicesModel.getVersionBeta() == null ) {
			throw new ParametersException("次版本号不能为空");
		}
		
		DevicesModel existModel = devicesModelMapper.selectInfoById(devicesModel.getModel());
		if(existModel == null) {
			throw new ParametersException("设备模型不存在");
		}
		
//		if(devicesModel.getVersionBase() < existModel.getVersionBase()) {
//			throw new ParametersException("主版本号不能小于当前主版本号");
//		}
//		if(devicesModel.getVersionAlpha() < existModel.getVersionAlpha()) {
//			throw new ParametersException("子版本号不能小于当前子版本号");
//		}
//		if(devicesModel.getVersionBeta() < existModel.getVersionBeta()) {
//			throw new ParametersException("次版本号不能小于当前次版本号");
//		}
		
		existModel.setVersionAlpha(devicesModel.getVersionAlpha());
		existModel.setVersionBase(devicesModel.getVersionBase());
		existModel.setVersionBeta(devicesModel.getVersionBeta());
		
//		if(devicesModel.getVersionAlpha() == existModel.getVersionAlpha() && devicesModel.getVersionBase() == existModel.getVersionBase() && devicesModel.getVersionBeta() == existModel.getVersionBeta()) {
//			existModel.setVersionBeta(existModel.getVersionBeta()+1);
//		}else {
//			existModel.setVersionAlpha(devicesModel.getVersionAlpha());
//			existModel.setVersionBase(devicesModel.getVersionBase());
//			existModel.setVersionBeta(devicesModel.getVersionBeta());
//			
//		}
		
		existModel.setUpgradeTime(new Date());
		
		existModel.setLastDrivePath(existModel.getDrivePath());
		existModel.setLastVersion(existModel.getVersion());
		
		existModel.setDrivePath(devicesModel.getDrivePath());
		existModel.setVersion(existModel.getVersionBase()+"."+existModel.getVersionAlpha()+"."+existModel.getVersionBeta());
		devicesModelMapper.update(existModel);
	}
	
	

	
}
